/*
 * Copyright (c) 2010, Jim Hollinger
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 *   * Redistributions of source code must retain the above copyright
 *     notice, this list of conditions and the following disclaimer.
 *   * Redistributions in binary form must reproduce the above copyright
 *     notice, this list of conditions and the following disclaimer in the
 *     documentation and/or other materials provided with the distribution.
 *   * Neither the name of Jim Hollinger nor the names of its contributors
 *     may be used to endorse or promote products derived from this
 *     software without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 *
 */

#include <io.h>
#include <conio.h>
#include <direct.h>

#include "debughelper/debughelper.h"
#include "properties/properties.h"
#include "wpsinterface.h"
#include "wpsopenjauscomponent.h"


const char *WPSJGP_VERSION = "V1.0a";

void usage() {
	printf("Usage: WPSJGP [config_filename]\n");
	exit(0);
}

int main(int argc, char *argv[]) {
#ifdef _DEBUG
	// turn on memoery leak detection
	// in a watch window, set the global variable
	// {,,msvcr80d.dll}_crtBreakAlloc 
	// to break on offending memory block allocation
	int tmpDbgFlag = _CrtSetDbgFlag(_CRTDBG_REPORT_FLAG);
	tmpDbgFlag |= _CRTDBG_LEAK_CHECK_DF;
	_CrtSetDbgFlag(tmpDbgFlag);
#endif

	std::string prop_filename = "wpsjgpConfig.txt";

	printf("WPS JAUS Global Pose sensor %s\n", WPSJGP_VERSION);

	// check command line arguments
	if (argc >= 3) {
		usage();
	} else if (argc >= 2) {
		if ((argv[1][0] == '?') || (argv[1][1] == '?')) {
			usage();
		}
		prop_filename = argv[1];
	}

	// load properties file
	prop::Properties properties;
	bool prop_flag = properties.load(prop_filename);
	std::string username        = properties.getProperty("username", "my_username");
	std::string realm           = properties.getProperty("realm", "my_realm");
	std::string tile_directory  = properties.getProperty("tile_directory", "./tiles/");
	std::string kml_filename    = properties.getProperty("kml_filename");
	if (prop_flag) {
		printf("Loaded properties file \"%s\"\n", prop_filename.c_str());
		properties.listUnrecognized(stderr, "Unrecognized properties:\n");
	} else {
		fprintf(stderr, "Failed to open properties file \"%s\"\n", prop_filename.c_str());
		if (properties.list(prop_filename, "# WPS JAUS Global Pose sensor config file\n")) {
			fprintf(stderr, "Created properties file \"%s\" with default values\n", prop_filename.c_str());
		}
	}

	// create tile directory if it does not exist
	if (!tile_directory.empty()) {
		if (_access(tile_directory.c_str(), 0) != 0) {
			if (_mkdir(tile_directory.c_str()) == 0) {
				fprintf(stderr, "Made tile directory \"%s\"\n", tile_directory.c_str());
			} else {
				fprintf(stderr, "Could not make tile directory \"%s\"\n", tile_directory.c_str());
			}
		}
	}

	// create wps interface
    wpsjgp::WpsInterface *wpsInterface = new wpsjgp::WpsInterface();
	if (wpsInterface->init(username, realm, tile_directory, kml_filename)) {
		wpsInterface->start(1.0);

		// create wps open jaus component
        wpsjgp::WpsOpenJausComponent *wpsComponent =
            new wpsjgp::WpsOpenJausComponent(wpsInterface);
		if (wpsComponent->init(10.0)) {
			wpsComponent->start();
			
			// wait for user to stop
			int c = 0;
			do {
				c = _getch();
			} while (c != 0x1B);

			// stop wps open jaus component
			wpsComponent->stop();
		}
		delete wpsComponent;
		wpsComponent = NULL;

		// stop wps interface
		wpsInterface->stop();
	}
	delete wpsInterface;
	wpsInterface = NULL;

	// exit app
	return 0;
}
